/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bankaccount;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author INEZ
 */
public class BankAccount {
    private int accId;
    private String accName;
    private double accBalance;
    private String accPss;
    public BankAccount() {
    }

    public BankAccount(int accId) {
        this.accId = accId;
    }

    public int getAccId() {
        return accId;
    }

    public void setAccId(int accId) {
        this.accId = accId;
    }

    public String getAccName() {
        return accName;
    }

    public void setAccName(String accName) {
        this.accName = accName;
    }

    public double getAccBalance() {
        return accBalance;
    }

    public void setAccBalance(double accBalance) {
        this.accBalance = accBalance;
    }

    public String getAccPss() {
        return accPss;
    }

    public void setAccPss(String accPss) {
        this.accPss = accPss;
    }
    public boolean addAccount(){
        int save = 0;
        try{
            Connection con = ConnectionBuilder.getConnection();
            String sqlCmd = "INSERT INTO BANKDATA(ACCID,ACCPSS,ACCNAME,ACCBALANCE) VALUES(?,?,?,?)";
            PreparedStatement stm = con.prepareStatement(sqlCmd);
            stm.setInt(1, this.accId);
            stm.setString(2, this.accPss);
            stm.setString(3, this.accName);
            stm.setDouble(4, this.accBalance);
            save = stm.executeUpdate();
            con.close();
        }catch(SQLException ex){
            System.out.println(ex);
        }
        return save > 0;
    }
    public static List<BankAccount> findById(int AccId){
        List lst = new ArrayList();
        BankAccount ba = null;
        try{
        Connection con = ConnectionBuilder.getConnection();
        String sql = "select * from BANKDATA where ACCID = "+AccId;
        Statement stm = con.createStatement();
        ResultSet rs = stm.executeQuery(sql);
        while(rs.next()){
            ba = new BankAccount(AccId);
            ba.setAccName(rs.getString("accName"));
            ba.setAccBalance(rs.getDouble("accBalance"));
            ba.setAccPss(rs.getString("ACCPSS"));
            lst.add(ba);
        }
        con.close();
        }catch(SQLException ex){
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Exception", 0);
        }
        return lst;
    }
    public static List<BankAccount> showAllId(){
        List<BankAccount> bas = new ArrayList();
        BankAccount ba = null;
        try{
            Connection con = ConnectionBuilder.getConnection();
            String sql = "SELECT * from BANKDATA";
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sql);
            while(rs.next()){
                ba = new BankAccount();
                ba.setAccId(rs.getInt("ACCID"));
                ba.setAccPss(rs.getString("ACCPSS"));
                ba.setAccName(rs.getString("ACCNAME"));
                ba.setAccBalance(rs.getDouble("ACCBALANCE"));
                bas.add(ba);
            }
        }catch(SQLException ex){
            System.out.println(ex.getStackTrace());
        }
        return bas;
    }
    public boolean deposit(double amount) throws BankAccountException{
        int depo = 0;
        if(amount < 0){
            throw new BankAccountException("Deposit less than 0");
        }else if((""+amount).equals("")){
            throw new BankAccountException("Please enter yout money");
        }else{  
            BankAccount ba;
            try{
                Connection con = ConnectionBuilder.getConnection();
                String sql = "SELECT ACCBALANCE FROM BANKDATA where ACCID = "+this.accId;
                Statement stm = con.createStatement();
                ResultSet rs = stm.executeQuery(sql);
                while(rs.next()){
                    this.accBalance = rs.getDouble("ACCBALANCE");
                }
            }catch(SQLException ex){
                System.out.println(ex);
            }
            try{
                Connection con = ConnectionBuilder.getConnection();
                String sql = "UPDATE BANKDATA SET ACCBALANCE = ? WHERE ACCID = "+this.accId;
                PreparedStatement stm = con.prepareStatement(sql);
                stm.setDouble(1, amount+this.accBalance);
                depo = stm.executeUpdate();
                con.close();
            }catch(SQLException ex){
                System.out.println(ex);
            }
            
        }
        return depo > 0;    
    }
    public boolean withdraw(double amount) throws BankAccountException{
        int wt = 0;
        if(amount > this.accBalance ){
            throw new BankAccountException("Withdraw over balance");           
        }else if(amount < 0){
            throw new BankAccountException("Withdraw less than 0");
        }else if((""+amount).equals("")){
            throw new BankAccountException("Please enter yout money");
        }else{
            BankAccount ba;
            try{
                Connection con = ConnectionBuilder.getConnection();
                String sql = "SELECT ACCBALANCE FROM BANKDATA where ACCID = "+this.accId;
                Statement stm = con.createStatement();
                ResultSet rs = stm.executeQuery(sql);
                while(rs.next()){
                    this.accBalance = rs.getDouble("ACCBALANCE");
                }
            }catch(SQLException ex){
                System.out.println(ex);
            }
            try{
                Connection con = ConnectionBuilder.getConnection();
                String sql = "UPDATE BANKDATA SET ACCBALANCE = ? WHERE ACCID = "+this.accId;
                PreparedStatement stm = con.prepareStatement(sql);
                stm.setDouble(1, this.accBalance-amount);
                wt = stm.executeUpdate();
                con.close();
            }catch(SQLException ex){
                System.out.println(ex);
            }
        }
        return wt > 0;
    }
    
    public void getValueAfterChange(){
        try{
            Connection con = ConnectionBuilder.getConnection();
            String sql = "SELECT * FROM BANKDATA where ACCID = "+this.accId;
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sql);
            while(rs.next()){
                this.accId = rs.getInt("ACCID");
                this.accName = rs.getString("ACCNAME");
                this.accPss = rs.getString("ACCPSS");
                this.accBalance = rs.getDouble("ACCBALANCE");
            }
        }catch(SQLException ex){
            System.out.println(ex);
        }
    }
    public static void DeleteAcc(int id){
        try{
            Connection con = ConnectionBuilder.getConnection();
            String sql = "DELETE FROM BANKDATA where ACCID = "+id;
            Statement stm = con.createStatement();
            if(stm.executeUpdate(sql)> 0){
                JOptionPane.showMessageDialog(null, "User : "+id+" has deleted");
            }else{
                JOptionPane.showMessageDialog(null, "User : "+id+" has not deleted");
            }
        }catch(SQLException ex){
            System.out.println(ex.getStackTrace());
        }
    }
    public void UpdateAcc(){
        try{
            Connection con = ConnectionBuilder.getConnection();
            String sql = "UPDATE BANKDATA set ACCPSS = ?,ACCNAME = ?,ACCBALANCE = ? where ACCID = ?";
            PreparedStatement stm = con.prepareStatement(sql);
            stm.setString(1, this.accPss);
            stm.setString(2, this.accName);
            stm.setDouble(3, this.accBalance);
            stm.setInt(4, this.accId);
            if(stm.executeUpdate() > 0){
                JOptionPane.showMessageDialog(null, "User id "+this.accId+" has been update");
            }else{
                JOptionPane.showMessageDialog(null, "User id "+this.accId+" has not update");
            }
        }catch(SQLException ex){
            System.out.println(ex.getStackTrace());
        }
    }
    
}
